//
//  TakePhotoViewController.m
//  MJAVFoundationDemo
//
//  Created by apple on 16/9/7.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "TakePhotoViewController.h"

@interface TakePhotoViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@end

@implementation TakePhotoViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title=@"上传图片";
    
    self.view.backgroundColor=[UIColor whiteColor];
    self.imageView.layer.borderColor=[UIColor blueColor].CGColor;
    self.imageView.layer.borderWidth=1.0;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}



- (IBAction)uploadImage:(id)sender
{
    //弹出来的提示框
    UIAlertController *actionSheet=[UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *takePhoto=[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
    {
        [self addCamera];
    }];
    UIAlertAction *chooseFromAlbum=[UIAlertAction actionWithTitle:@"去相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [self addPhotoLiabrary];
    }];
    UIAlertAction *cancel=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    [actionSheet addAction:takePhoto];
    [actionSheet addAction:chooseFromAlbum];
    [actionSheet addAction:cancel];
    [self presentViewController:actionSheet animated:YES completion:nil];

}
-(void)addCamera
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        UIImagePickerController *imgPicker=[[UIImagePickerController alloc]init];
        imgPicker.allowsEditing=YES;
        imgPicker.sourceType=UIImagePickerControllerSourceTypeCamera;//图片来源于相机
        imgPicker.delegate=self;
        [self presentViewController:imgPicker animated:YES completion:nil];
    }else
    {
       [MBProgressHUD showError:@"请去设置里面允许应用访问相机"];
    }
}
-(void)addPhotoLiabrary
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
    {
        UIImagePickerController *imgPicker=[[UIImagePickerController alloc]init];
        imgPicker.editing=YES;
        imgPicker.allowsEditing=YES;//允许图片可编辑
        imgPicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;//图片来源于相册
        imgPicker.delegate=self;
        [self presentViewController:imgPicker animated:YES completion:nil];
    }else
    {
        [MBProgressHUD showError:@"请去设置里面允许应用访问相册"];
    }
}
//图片选择器的代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
   [picker dismissViewControllerAnimated:YES completion:^{
       UIImage *editdImage,*originalImage;
       //获得编辑后的图片
       editdImage=[info objectForKey:UIImagePickerControllerEditedImage];
       //[MBProgressHUD showMessage:@"图片正在上传中..." toView:self.view];
       //设置要上传的图片为编辑后的图片
       _imageView.image=editdImage;
       
       if (picker.sourceType==UIImagePickerControllerSourceTypeCamera) {
           //获得原始图片,保存到相册
           originalImage=[info objectForKey:UIImagePickerControllerOriginalImage];
           UIImageWriteToSavedPhotosAlbum(originalImage, self, nil, NULL);
       }
   }];
    //[MBProgressHUD hideHUDForView:self.view animated:YES];
    
}
//返回按钮

@end
